
package pho.helper;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Peter
 *
 * Helper Class for various Date Conversion routines, pure static cannot be instantiated
 */
public class DateConverter
{
	private DateConverter ()
	{
	}

	/**
	 * This helper converts a string which should contain a valid date representation from the input format
	 * to the output format and returns it as a byte array. In case the value cannot be converted an empty byte
	 * array is returned.
	 * @param tagValue a string containing a date or time in the formatIn representation
	 * @param formatIn a date format string for parsing the incoming date/time string
	 * @param formatOut the date format string the tagValue needs to be converted to for output
	 * @return a byte array containing the binary representation of the date / time value converted to formatOut
	 */
	public static byte [] convertDateFormatToByteArray (String tagValue, String formatIn, String formatOut)
	{
		DateFormat df =	new SimpleDateFormat(formatIn);
		Date date;
		try
		{
			date = df.parse(tagValue);
			df = new SimpleDateFormat(formatOut);
			return df.format(date).getBytes();
		}
		catch (ParseException e)
		{
			// System.out.println (e.getMessage());
		}
		return new byte [0];
	}
	
	/**
	 * This helper converts a string which should contain a valid date representation from the input format
	 * to the output format and returns it as a string. In case the value cannot be converted an empty string
	 * is returned.
	 * @param tagValue a string containing a date or time in the formatIn representation
	 * @param formatIn a date format string for parsing the incoming date/time string
	 * @param formatOut the date format string the tagValue needs to be converted to for output
	 * @return a byte array containing the binary representation of the date / time value converted to formatOut
	 */
	public static String convertDateFormat (String tagValue, String formatIn, String formatOut)
	{
		//@TODO Check how we adjust the locale of the time format to the locale of the application
		DateFormat df =	new SimpleDateFormat(formatIn);
		Date date;
		try
		{
			date = df.parse(tagValue);
			df = new SimpleDateFormat(formatOut);
			return df.format(date);
		}
		catch (ParseException e)
		{
			//System.out.println (e.getMessage());
		}
		return "";
	}	
}
